import 'dart:io';

import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';

import '../tools_base/cache/dio_file_service.dart';

/// 视频缓存
class VideoCacheManager extends CacheManager {
  static const key = "videoCache";

  static VideoCacheManager? _instance;

  factory VideoCacheManager() {
    _instance ??= VideoCacheManager._(fileService: DioFileService());
    return _instance!;
  }

  /// 这里可以构建一个文件fileFetch
  VideoCacheManager._({required FileService fileService})
      : super(Config(key, stalePeriod: const Duration(days: 100), maxNrOfCacheObjects: 200, fileService: DioFileService()));

  // factory VideoCacheManager() => _getInstance();
  //
  // static VideoCacheManager get instance => _getInstance();
  // static VideoCacheManager? _instance;
  //
  // static VideoCacheManager _getInstance() {
  //   _instance ??= VideoCacheManager._internal();
  //   return _instance!;
  // }
  //
  // VideoCacheManager._internal(){
  //
  // };

  /// 缓存存储路径
  Future<String?> getFilePath() async {
    var dir = await getCommonDir();
    if (dir != null) {
      return path.join(dir.path, key);
    }
  }

  /// 获取公用的dir目录
  static Future<Directory?> getCommonDir() async {
    Directory? dir;
    if (Platform.isIOS) {
      dir = await getTemporaryDirectory();
    } else if (Platform.isAndroid) {
      dir = await getExternalStorageDirectory();
    } else if (Platform.isFuchsia) {
      dir = await getApplicationSupportDirectory();
    } else if (Platform.isMacOS) {
      dir = await getApplicationSupportDirectory();
    } else if (Platform.isLinux) {
      dir = await getApplicationSupportDirectory();
    } else if (Platform.isWindows) {
      dir = await getApplicationSupportDirectory();
    } else {
      dir = await getExternalStorageDirectory();
    }
    return dir;
  }
}
